package pers.zw.study.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;

/**
 * Demo 5 and 6 together show the dispatcher between two servlets
 */
@WebServlet(name = "ServletDemo5", urlPatterns = "/ServletDemo5")
public class ServletDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = "<h1><font color = 'red'>zzww</font></h1>";

        response.setStatus(SC_NOT_FOUND);
        response.getOutputStream().write(data.getBytes());
        ServletContext context = this.getServletContext();
        RequestDispatcher rd = context.getRequestDispatcher("/ServletDemo6"); //获取请求转发对象
        rd.forward(request, response); //forward 实现请求转发
    }
}
